package com.ae.shield.common.items.shieldFittings.rectifier;

import com.ae.shield.common.tag.ModTags;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import java.util.List;

public class EmergencyLifeSupportSystem extends RectifierMakerBase {
    public EmergencyLifeSupportSystem() {
        super(3);
    }

    @Override
    public void addRectifierInformation(ItemStack stack, List<ITextComponent> components) {
        ITextComponent cost = new StringTextComponent(Integer.toString(energyCost(stack))).mergeStyle(TextFormatting.BLUE);
        components.add(new TranslationTextComponent("tooltip.ae.shield.energy_cost", cost)
                .mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.life_support")
                .mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.rectifier_maker_base.level_up3")
                .mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.rectifier_maker_base.count")
                .mergeStyle(TextFormatting.GRAY)
                .appendString("4"));
    }

    @Override
    public float onEntityHurt(LivingEntity livingEntity, ItemStack stack, float amount, DamageSource source, IEnergyStorage energyCap) {
        int cost = energyCost(stack);
        if (energyCap.extractEnergy(cost, true) == cost) {
            energyCap.extractEnergy(cost, false);
            return 0;
        }
        return 0;
    }

    @Override
    public boolean onEntityDeath(ItemStack stack, LivingEntity livingEntity, DamageSource source, IEnergyStorage energyCap) {
        int cost = energyCost(stack);
        if (energyCap.extractEnergy(cost, true) == cost) {
            energyCap.extractEnergy(cost, false);
            return true;
        }
        return false;
    }

    public int energyCost(ItemStack stack) {
        return 10000 - (getLevel(stack) - 1) * 2000;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, @Nonnull Hand handIn) {
        ItemStack heldItem = playerIn.getHeldItemMainhand();
        if (handIn == Hand.MAIN_HAND) {
            ItemStack stack = playerIn.getHeldItemOffhand();
            if (!worldIn.isRemote) {
                if (stack.getCount() > 3 && ModTags.Forge.Item.CIRCUITS_ELITE.contains(stack.getItem()) && getLevel(heldItem) < maxLevel) {
                    stack.shrink(4);
                    setLevel(heldItem, getLevel(heldItem) + 1);
                    worldIn.playSound(null, playerIn.chasingPosX, playerIn.chasingPosY, playerIn.chasingPosZ, SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 5f, 1f);
                    return ActionResult.resultSuccess(heldItem);
                }
            }
        }
        return ActionResult.resultPass(playerIn.getHeldItem(handIn));
    }
}
